package classes.view.component
{
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.ENumber;
	import classes.controller.EPass;
	import classes.controller.ESendJS;
	import classes.model.VOData;
	import classes.model.VOUserInfo;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quart;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class PopupFriend_ListContainer extends Sprite
	{
		private var _totalPage:int;
		private var _currentPage:int;
		private var _screenNum:int;  
		private var _screenTotalNum:int = 8;// 화면에 표시될 갯수-1.
		private var _listData:Array;
		private var _list:Array;
		private var _listCont:Sprite;
		private var _paging:PopupPagingContainer;
		private var _viewType:Boolean;
		private var _popupItem:PopupFriend_popupcont;
		private var _currentSelectedItem:int;
		
		public function PopupFriend_ListContainer()
		{
			super();
			setLayOut();
			init();
		}
		
		/**
		 * 
		 * 친구의 상태를 서버와 통신하는 곳,
		 * 
		 **/
		
		private function sendStatus($id:int, $num:int, $passStatus:int, $roomID:int):void
		{
			//_popupItem.mouseChildren = false;
			//_popupItem.mouseEnabled  = false;
			DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_JOINROOMSTATE,	onJoinRoomState);
			
			if($passStatus == 1){
				//비공개
				_popupItem.popupSelected = 3;
				_popupItem.roomID 		 = $roomID;
				_popupItem.visible 		 = true;
				_popupItem.alpha		 = 0;
				_popupItem.y 			 = (_currentSelectedItem*34) +5;
				TweenLite.to(_popupItem, 0.4, {alpha:1, y:(_currentSelectedItem*34), ease:Cubic.easeOut});
			}else if($passStatus == 0){
				//공개
				var obj:Object;
				obj = {
					"data":[
						{
							"id":VOUserInfo.getDataInstance().data.id
							,"roomID":$roomID
							,"password":""
						}
					]
				}
				
				DAOSendFlash.getLoadInstance().setData("joinRoom", obj);
			}
		}
		
		
		private function onJoinRoomState(e:ESendJS):void
		{
			joinRoomState = e._obj.roomState
		}
		private function onPassIn(e:EPass):void
		{
			var obj:Object;
			obj = {
				"data":[
					{
						"id":VOUserInfo.getDataInstance().data.id
						,"roomID":e._roomID
						,"password":e._password
					}
				]
			}
			
			DAOSendFlash.getLoadInstance().setData("joinRoom", obj);
		}
		private function set joinRoomState($state:int):void
		{
			DAOSendJS.getLoadInstance().removeEventListener(ESendJS.EVENT_JOINROOMSTATE,	onJoinRoomState);
			
			switch($state){
				case 1:
					//입장.
					_popupItem.popupClose();
					dispatchEvent(new Event("withGame"));
					break;
				case 2:
					//비번틀림.
					_popupItem.incorrectPass();
					break;
				case 3:
					//인원수 초과.
					_popupItem.popupSelected = 0;
					_popupItem.visible 		 = true;
					_popupItem.alpha		 = 0;
					_popupItem.y 			 = (_currentSelectedItem*34) +5;
					TweenLite.to(_popupItem, 0.4, {alpha:1, y:(_currentSelectedItem*34), ease:Cubic.easeOut});
					break;
				case 4:
					//게임진행.
					_popupItem.popupSelected = 2;
					_popupItem.visible 		 = true;
					_popupItem.alpha		 = 0;
					_popupItem.y 			 = (_currentSelectedItem*34) +5;
					TweenLite.to(_popupItem, 0.4, {alpha:1, y:(_currentSelectedItem*34), ease:Cubic.easeOut});
					break;
				case 5:
					//방이 없습니다.
					_popupItem.popupSelected = 4;
					_popupItem.visible 		 = true;
					_popupItem.alpha		 = 0;
					_popupItem.y 			 = (_currentSelectedItem*34) +5;
					TweenLite.to(_popupItem, 0.4, {alpha:1, y:(_currentSelectedItem*34), ease:Cubic.easeOut});
					break;
				default :
					
					break;
			}
			
		}
		
		
		public function set pagingY($param:int):void{_paging.y = $param;}
		public function set viewNum($param:int):void{_screenTotalNum = $param;}
		public function set viewType($param:Boolean):void{_viewType = $param;}
		
		public function set listOn($obj:Object):void
		{
			_paging.visible = true;
			if(_listData==null) _listData = new Array();
			for(var i:int = 0 ; i < $obj.length ; i++){
				
				if(_listData[_totalPage] ==null) _listData[_totalPage] = new Array();
				
				_listData[_totalPage].push({listID:i, name:$obj[i].nick, id:$obj[i].id, currentLoc:$obj[i].roomID, subject:$obj[i].subject, secret:$obj[i].secret});
				if(_screenNum < _screenTotalNum){
					_screenNum++;
				}else{
					_screenNum=0;
					_totalPage++;
				}
			}
		}
		
		public function set pageOn($page:int):void
		{
			resetPage();
			_listCont.alpha = 0;
			_listCont.y = -5;
			if(_list == null) _list = new Array();
			
			
			_currentPage = $page;
			for(var i:int = 0 ; i <_listData[$page-1].length ; i++){
				if(_viewType == 0){
					_list[i] = new PopupFriend_listItem();
					
					_list[i].itemIn(i, _listData[$page-1][i].name, _listData[$page-1][i].id, _listData[$page-1][i].currentLoc, _listData[$page-1][i].subject, _listData[$page-1][i].secret);
				
				}else{
					_list[i] = new PopupFriend_addItem();
					_list[i].itemIn(i, _listData[$page-1][i].name, _listData[$page-1][i].id, _listData[$page-1][i].currentLoc, _listData[$page-1][i].subject, _listData[$page-1][i].secret);
					
					
				}
				_list[i].addEventListener(ENumber.EVENT_NUMBER,	onClickListChild);	
				
				_list[i].y = i*34;
				
				this._listCont.addChild(_list[i]);
			}
			
			if(_listData[_listData.length-1].length==_screenTotalNum+1){
				_paging.pageOn($page, _totalPage);
				
			}else{
				_paging.pageOn($page, _totalPage+1);	
				
			}
			
			TweenLite.to(_listCont, 0.4, {alpha:1, y:0, ease:Quart.easeOut});
		}
		
		private var _currentPopupSelected:int;
		private function onClickListChild(e:ENumber):void
		{
			_currentSelectedItem = e.currentTarget.id;
			//trace(e.currentTarget.id + ">>>>>" + e._num);
			switch(e._num){
				case 0:
					//친구 방의 상태.
					sendStatus(e.currentTarget.id, e._num, e.currentTarget.secret, e.currentTarget.roomID);
					
					break;
				case 1:
					//삭제하시겠습니까?
					if((_popupItem.popupSelected == 1) && (_currentPopupSelected == e.currentTarget.id)){
						_popupItem.popupClose();
						_popupItem.popupSelected = 4;
					}else{
						_popupItem.clickFriend   = _listData[_currentPage-1][e.currentTarget.id].id;
						_popupItem.popupSelected = 1;
						_popupItem.visible 		 = true;
						_popupItem.alpha		 = 0;
						_popupItem.y 			 = (e.currentTarget.id*34) +5;
						TweenLite.to(_popupItem, 0.4, {alpha:1, y:(e.currentTarget.id*34), ease:Cubic.easeOut});
					}
					
					break;

				case 4:
					//친구 추가.
					trace("친구 추가");
					
					
					var obj:Object = {
					"data":[
							{
								"fr_id":e.currentTarget.friendId
								,"mb_id":VOUserInfo.getDataInstance().data.id
							}
						]
					}
					
					DAOSendFlash.getLoadInstance().setData('friendAdd', obj);
					
					
					
					
					break;
			}
			_currentPopupSelected = e.currentTarget.id;
		}
		
		private function setLayOut():void
		{
			this._popupItem		=	this.getChildByName('friend_popup_item')		as PopupFriend_popupcont;
			this._listCont		=   new Sprite();
			this._paging		= 	this.getChildByName('poppaging_mc')				as PopupPagingContainer;
			this.addChildAt(this._listCont, 0);
		}
		private function init():void
		{
			this._paging.addEventListener(ENumber.EVENT_NUMBER,			onPagingClick);
			this._popupItem.addEventListener(EPass.EVENT_PASSWORD, 		onPassIn);
		}
		private function onPagingClick(e:ENumber):void
		{
			pageOn = e._num;
		}
		
		private function resetPage():void
		{
			_popupItem.visible 			= false;
			_popupItem.alpha 			= 0;
			_popupItem.popupSelected 	= -1;
			
			if(_list != null){
				if(_list.length > 0 ){
					while (this._listCont.numChildren > 0) {
						if(_list[_list.length-1].hasEventListener(ENumber.EVENT_NUMBER))_list[_list.length-1].removeEventListener(ENumber.EVENT_NUMBER,	onClickListChild);
						this._listCont.removeChildAt(0);
					}
					_list.splice(0, _list.length-1);
				}
				_list = null;
			}
		}
		
		
		public function resetList():void
		{
			_popupItem.visible 			= false;
			_popupItem.alpha 			= 0;
			_popupItem.popupSelected 	= -1;
			
			_paging.visible 	= false;
			
			_totalPage   = 0;
			_currentPage = 0;
			_screenNum   = 0;
			
			resetPage();
			
			if(_listData != null){
				for(var i:int = 0 ; i < _listData.length ; i++){
					_listData[i].splice(0, _listData[i].length-1);
					_listData[i] = null;
				}
				
			
				_listData.splice(0, _listData.length-1);
				_listData = null;
			}
			
			
		}
	}
}